/*
* Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:flutter/material.dart';

class MyWidget extends YPage {
  var title = "";
  var url =
      "https://q2.itc.cn/q_70/images03/20240807/9efb7d3e616440c6ab1d7e1d9b9be14f.jpeg";
  bool _showFirst = true;

  @override
  void yOnCreate() {
    setState(() => title = yGetAppName());
    yBroadcast_register("MyBroadcast", (params) => yToast(params!["msg"]));
  }

  @override
  void initState() {
    super.initState();
    yBroadcast_send("MyBroadcast", params: {"msg": "initState"});
  }

  @override
  void yOnDestory() {
    super.yOnDestory();
    yBroadcast_send("MyBroadcast", params: {"msg": "yOnDestory"});
  }

  @override
  Future yOnYFreeCall(String method, Map arguments) {
    return super.yOnYFreeCall(method, arguments);
  }

  @override
  void yOnReceiveBroadcast(String broadCastName, Map arguments) {
    super.yOnReceiveBroadcast(broadCastName, arguments);
  }

  @override
  bool ySafeAreaBottom() {
    return false;
  }

  Widget view_button(String title, Function() onTap) {
    return YButton(
      child: Text(title,
          style: const TextStyle(
              color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      color: YConfig.themeColor,
      onTap: onTap,
    );
  }

  void showCustomDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Custom Dialog'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                decoration: InputDecoration(
                  hintText: 'Tap outside to close keyboard',
                ),
              ),
              SizedBox(height: 16.0),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      yCloseInputMethod();
                    },
                    child: Text('yCloseInputMethod'),
                  ),
                ],
              )
            ],
          ),
        );
      },
    );
  }

  void showCustomDialog_YCIMGestureDetector(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return YCIMGestureDetector(
          context,
          AlertDialog(
            title: Text('Custom Dialog'),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextField(
                  decoration: InputDecoration(
                    hintText: 'Tap outside to close keyboard',
                  ),
                ),
                SizedBox(height: 16.0),
                ElevatedButton(
                  onPressed: () {
                    Navigator.of(context).pop(); // Close the dialog
                  },
                  child: Text('关闭弹框'),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: Colors.white,
      child: SingleChildScrollView(
          child: Column(
        children: <Widget>[
          YAppBar(title: title, canBack: false),
          Container(
            width: yScreenSize().width,
            padding: const EdgeInsets.all(5),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              direction: Axis.vertical,
              children: [
                view_button("yCloseInputMethod", () {
                  showCustomDialog(context);
                }),

                view_button("YCIMGestureDetector", () {
                  showCustomDialog_YCIMGestureDetector(context);
                }),
              ],
            ),
          ),
        ],
      )),
    );
  }
}
